//
//  UITextField+Extension.m
//  Practice
//
//  Created by yons on 14.8.20.
//  Copyright © 2020 yons. All rights reserved.
//

#import "UITextField+Extension.h"

@implementation UITextField (Extension)

/// placeholder
/// @param placeholder placeholder
+ (UITextField *)textFieldWithPlaceholder:(NSString *)placeholder {
    UITextField *textField = [[UITextField alloc] init];
    textField.placeholder = placeholder;
    return textField;
}

/// placeholder+文本颜色+文本大小+是否显示清除按钮
/// @param placeholder placeholder
/// @param textColor 文本颜色
/// @param size 文本大小
+ (UITextField *)textFieldWithPlaceholder:(NSString *)placeholder textColor:(UIColor *)textColor fontSize:(CGFloat)size weight:(UIFontWeight)weight {
    UITextField *textField = [self textFieldWithPlaceholder:placeholder];
    textField.textColor = textColor;
    textField.font = [UIFont systemFontOfSize:size weight:weight];
    return textField;
}

/// placeholder+文本颜色+文本大小+是否显示清除按钮
/// @param placeholder placeholder
/// @param textColor 文本颜色
/// @param size 文本大小
/// @param showClearModel 是否显示清除按钮
+ (UITextField *)textFieldWithPlaceholder:(NSString *)placeholder textColor:(UIColor *)textColor fontSize:(CGFloat)size weight:(UIFontWeight)weight isShowClearModel:(BOOL)showClearModel {
    UITextField *textField = [self textFieldWithPlaceholder:placeholder textColor:textColor fontSize:size weight:weight];
    if (showClearModel) {
        textField.clearButtonMode = UITextFieldViewModeAlways;
    } else {
        textField.clearButtonMode = UITextFieldViewModeNever;
    }
    
    return textField;
}

/// placeholder+文本颜色+文本大小+是否显示清除按钮+按钮点击事件
/// @param placeholder placeholder
/// @param textColor 文本颜色
/// @param size 文本大小
/// @param showClearModel 是否显示清除按钮
/// @param target target
/// @param action 方法
/// @param controlEvents 点击事件
+ (UITextField *)textFieldWithPlaceholder:(NSString *)placeholder textColor:(UIColor *)textColor fontSize:(CGFloat)size weight:(UIFontWeight)weight isShowClearModel:(BOOL)showClearModel addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents {
    UITextField *textField = [self textFieldWithPlaceholder:placeholder textColor:textColor fontSize:size weight:weight isShowClearModel:showClearModel];
    [textField addTarget:target action:action forControlEvents:controlEvents];
    return textField;
}

/// placeholder+文本颜色+文本大小+剪切半径
/// @param placeholder placeholder
/// @param textColor 文本颜色
/// @param size 文本大小
/// @param radius 剪切半径
+ (UITextField *)textFieldWithPlaceholder:(NSString *)placeholder textColor:(UIColor *)textColor fontSize:(CGFloat)size weight:(UIFontWeight)weight radius:(CGFloat)radius {
    UITextField *textField = [self textFieldWithPlaceholder:placeholder textColor:textColor fontSize:size weight:weight];
    textField.layer.cornerRadius = radius;
    textField.clipsToBounds = YES;
    return textField;
}

/// placeholder+文本颜色+文本大小+剪切半径+是否显示清除按钮
/// @param placeholder placeholder
/// @param textColor 文本颜色
/// @param size 文本大小
/// @param radius 剪切半径
/// @param showClearModel 是否显示清除按钮
+ (UITextField *)textFieldWithPlaceholder:(NSString *)placeholder textColor:(UIColor *)textColor fontSize:(CGFloat)size weight:(UIFontWeight)weight radius:(CGFloat)radius isShowClearModel:(BOOL)showClearModel {
    UITextField *textField = [self textFieldWithPlaceholder:placeholder textColor:textColor fontSize:size weight:weight radius:radius];
    if (showClearModel) {
        textField.clearButtonMode = UITextFieldViewModeAlways;
    } else {
        textField.clearButtonMode = UITextFieldViewModeNever;
    }
    return textField;
}

/// placeholder+文本颜色+文本大小+剪切半径+是否显示清除按钮+按钮点击事件
/// @param placeholder placeholder
/// @param textColor 文本颜色
/// @param size 文本大小
/// @param radius 剪切半径
/// @param showClearModel 是否显示清除按钮
/// @param target target
/// @param action 方法
/// @param controlEvents 点击事件
+ (UITextField *)textFieldWithPlaceholder:(NSString *)placeholder textColor:(UIColor *)textColor fontSize:(CGFloat)size weight:(UIFontWeight)weight radius:(CGFloat)radius isShowClearModel:(BOOL)showClearModel addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents {
    UITextField *textField = [self textFieldWithPlaceholder:placeholder textColor:textColor fontSize:size weight:weight radius:radius isShowClearModel:showClearModel];
    [textField addTarget:target action:action forControlEvents:controlEvents];
    return textField;
}

/// placeholder+文本颜色+文本大小+剪切半径+边框宽度+边框颜色
/// @param placeholder placeholder
/// @param textColor 文本颜色
/// @param size 文本大小
/// @param radius 剪切半径
/// @param borderWidth 边框宽度
/// @param borderColor 边框颜色
+ (UITextField *)textFieldWithPlaceholder:(NSString *)placeholder textColor:(UIColor *)textColor fontSize:(CGFloat)size weight:(UIFontWeight)weight radius:(CGFloat)radius borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor {
    UITextField *textField = [self textFieldWithPlaceholder:placeholder textColor:textColor fontSize:size weight:weight radius:radius];
    textField.layer.borderWidth = borderWidth;
    textField.layer.borderColor = borderColor.CGColor;
    return textField;
}

/// placeholder+文本颜色+文本大小+剪切半径+边框宽度+边框颜色+是否显示清除按钮
/// @param placeholder placeholder
/// @param textColor 文本颜色
/// @param size 文本大小
/// @param radius 剪切半径
/// @param borderWidth 边框宽度
/// @param borderColor 边框颜色
/// @param showClearModel 是否显示清除按钮
+ (UITextField *)textFieldWithPlaceholder:(NSString *)placeholder textColor:(UIColor *)textColor fontSize:(CGFloat)size weight:(UIFontWeight)weight radius:(CGFloat)radius borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor isShowClearModel:(BOOL)showClearModel {
    UITextField *textField = [self textFieldWithPlaceholder:placeholder textColor:textColor fontSize:size weight:weight radius:radius borderWidth:borderWidth borderColor:borderColor];
    if (showClearModel) {
        textField.clearButtonMode = UITextFieldViewModeAlways;
    } else {
        textField.clearButtonMode = UITextFieldViewModeNever;
    }
    return textField;
}

/// placeholder+文本颜色+文本大小+剪切半径+边框宽度+边框颜色+是否显示清除按钮+按钮点击事件
/// @param placeholder placeholder
/// @param textColor 文本颜色
/// @param size 文本大小
/// @param radius 剪切半径
/// @param borderWidth 边框宽度
/// @param borderColor 边框颜色
/// @param showClearModel 是否显示清除按钮
/// @param target target
/// @param action 方法
/// @param controlEvents 点击事件
+ (UITextField *)textFieldWithPlaceholder:(NSString *)placeholder textColor:(UIColor *)textColor fontSize:(CGFloat)size weight:(UIFontWeight)weight radius:(CGFloat)radius borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor isShowClearModel:(BOOL)showClearModel addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents {
    UITextField *textField = [self textFieldWithPlaceholder:placeholder textColor:textColor fontSize:size weight:weight radius:radius borderWidth:borderWidth borderColor:borderColor];
    [textField addTarget:target action:action forControlEvents:controlEvents];
    return textField;
}

/// 左边leftView
/// @param frame 大小
- (UITextField *)textFieldWithLeftImageViewFrame:(CGRect)frame {
    UIView *leftView = [[UIView alloc] initWithFrame:frame];
    self.leftViewMode = UITextFieldViewModeAlways;
    self.leftView = leftView;
    return self;
}

/// 左边leftView
/// @param frame 大小
/// @param named 图片名
- (UITextField *)textFieldWithLeftImageViewFrame:(CGRect)frame imageNamed:(NSString *)named {
    UIView *leftView = [[UIView alloc] initWithFrame:frame];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:named]];
    self.leftViewMode = UITextFieldViewModeAlways;
    [leftView addSubview:imgView];
    self.leftView = leftView;
    imgView.center = self.leftView.center;
    return self;
}

/// 右边leftView
/// @param frame 大小
/// @param named 图片名
- (UITextField *)textFieldWithRightImageViewFrame:(CGRect)frame imageNamed:(NSString *)named {
    UIView *rightView = [[UIView alloc] initWithFrame:frame];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:named]];
    self.rightViewMode = UITextFieldViewModeAlways;
    [rightView addSubview:imgView];
    self.rightView = rightView;
    imgView.center = self.rightView.center;
    return self;
}

/// 右边leftView
/// 右边leftView
/// @param frame 大小
/// @param customeView 自定义View
- (UITextField *)textFieldWithRightCustomeViewFrame:(CGRect)frame customeView:(UIView *)customeView {
    UIView *rightView = [[UIView alloc] initWithFrame:frame];
    customeView.frame = frame;
    [rightView addSubview:customeView];
    self.rightViewMode = UITextFieldViewModeAlways;
    self.rightView = rightView;
    customeView.center = self.rightView.center;
    return self;
}

@end
